package com.huli.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huli.mapper.OrganizationUserMapper;
import com.huli.pojo.SysOrganization;
import com.huli.service.OrganizationUserService;
import com.huli.vo.OrganizationUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrganizationUserServiceImpl implements OrganizationUserService {
    // 注入OrganizationUserMapper
    @Autowired
    private OrganizationUserMapper organizationUserMapper;
    @Override
    public OrganizationUserVo organizationUserList(OrganizationUserVo organizationUserVo) {
        // 使用PageHelper插件进行分页
        PageHelper.startPage(organizationUserVo.getPageNum(),organizationUserVo.getPageSize());
        // 调用Mapper接口查询组织用户列表
        List<SysOrganization> sysOrganizationList = organizationUserMapper.organizationUserList(organizationUserVo);
        // 将查询结果封装到PageInfo对象中
        PageInfo<SysOrganization> sysOrganizationPageInfo = new PageInfo<>(sysOrganizationList);
        // 返回封装后的结果
        return new OrganizationUserVo(sysOrganizationPageInfo.getTotal(),sysOrganizationPageInfo.getList());
    }

    @Override
    public List<SysOrganization> organizationUserListByOrgName(String orgName) {
        // 根据组织名称查询组织用户列表
        return organizationUserMapper.organizationUserListByOrgName(orgName);
    }

    @Override
    public void deleteOrganizationUser(Long id) {
        // 根据id删除组织用户
        organizationUserMapper.deleteOrganizationUser(id);
    }

    @Override
    public void addOrganization(SysOrganization sysOrganization) {
        // 设置创建时间
        sysOrganization.setCreateTime(new Date());
        // 调用Mapper接口添加组织
        organizationUserMapper.addOrganization(sysOrganization);
    }

    @Override
    public void updateOrganization(SysOrganization sysOrganization) {
        sysOrganization.setUpdateTime(new Date());
        organizationUserMapper.updateOrganization(sysOrganization);
    }

    @Override
    public SysOrganization selectOrganizationById(Long id) {
        SysOrganization sysOrganization = organizationUserMapper.selectOrganizationById(id);
        return sysOrganization;
    }

    @Override
    public SysOrganization organizationById(Long id) {
        return organizationUserMapper.getOrganizationById(id);
    }

    @Override
    public List<SysOrganization> findAll() {
        List<SysOrganization> all = organizationUserMapper.findAll();
        return all;
    }

}